/** index.js By Beginner Emain:zheng_jinfan@126.com HomePage:http://www.zhengjinfan.cn */

var tab;

layui.config({
    base: 'js/',
    version: new Date().getTime()
}).use(['element', 'layer', 'navbar', 'tab'], function () {
    var element = layui.element,
        $ = layui.jquery,
        layer = layui.layer,
        navbar = layui.navbar();
    tab = layui.tab({
        elem: '.admin-nav-card' //设置选项卡容器
        ,
        //maxSetting: {
        //	max: 5,
        //	tipMsg: '只能开5个哇，不能再开了。真的。'
        //},
        contextMenu: true,
        onSwitch: function (data) {
            // console.log(data.id); //当前Tab的Id
            // console.log(data.index); //得到当前Tab的所在下标
            // console.log(data.elem); //得到当前的Tab大容器
            //
            // console.log(tab.getCurrentTabId())
        },
        closeBefore: function (obj) { //tab 关闭之前触发的事件
            // console.log(obj);
            //obj.title  -- 标题
            //obj.url    -- 链接地址
            //obj.id     -- id
            //obj.tabId  -- lay-id
            if (obj.title === 'BTable') {
                layer.confirm('确定要关闭' + obj.title + '吗?', { icon: 3, title: '系统提示' }, function (index) {
                    //因为confirm是非阻塞的，所以这里关闭当前tab需要调用一下deleteTab方法
                    tab.deleteTab(obj.tabId);
                    layer.close(index);
                });
                //返回true会直接关闭当前tab
                return false;
            }else if(obj.title==='表单'){
                layer.confirm('未保存的数据可能会丢失哦，确定要关闭吗?', { icon: 3, title: '系统提示' }, function (index) {
                    tab.deleteTab(obj.tabId);
                    layer.close(index);
                });
                return false;
            }
            return true;
        }
    });
    //iframe自适应
    $(window).on('resize', function () {
        var $content = $('.admin-nav-card .layui-tab-content');
        $content.height($(this).height() - 147);
        $content.find('iframe').each(function () {
            $(this).height($content.height());
        });
    }).resize();


    //
    function setTree(nodesList,permissList) {
        $.each(nodesList,function (index,value) {
            $.each(permissList,function (index2,value2) {
                if(value.id == value2){
                    nodesList[index].checked = true;
                }
            })
        })
    }
$.ajax({
    url: contextPath+"/permission",
    type: "GET",
    contentType:"application/json;charset=utf-8",
    dataType:"json",
    success: function(res) {


        var rolejson = {};
        // console.log(res.list);
        setTree(jcsjglNodes,res.list);
        setTree(ywglNodes,res.list);
        setTree(yyfxNodes,res.list);
        setTree(cwglNodes,res.list);
        setTree(xtglNodes,res.list);


        rolejson.jscjgl = jcsjglNodes;
        rolejson.ywgl = ywglNodes;
        rolejson.yyfx =yyfxNodes;
        rolejson.cwgl =  cwglNodes;
        rolejson.xtgl = xtglNodes;

        // console.log(rolejson);

        //组装nav json
        var navTest = new Array();

        if(rolejson.jscjgl[0].checked){
            var t1 = {
                "title": "基础数据管理",
                "icon": "fa-book",
                "spread": true,
            };
            navTest.push(t1);
            var children = new Array();

//CURD 权限
            var role11 = "R="+rolejson.jscjgl[6].checked +"&C="+rolejson.jscjgl[7].checked +"&U="+rolejson.jscjgl[8].checked +"&D="+rolejson.jscjgl[9].checked;
            var role12 = "R="+rolejson.jscjgl[10].checked +"&C="+rolejson.jscjgl[11].checked +"&U="+rolejson.jscjgl[12].checked +"&D="+rolejson.jscjgl[13].checked;
            var role13 = "R="+rolejson.jscjgl[14].checked +"&C="+rolejson.jscjgl[15].checked +"&U="+rolejson.jscjgl[16].checked +"&D="+rolejson.jscjgl[17].checked;

            var role14 = "R="+rolejson.jscjgl[18].checked +"&C="+rolejson.jscjgl[19].checked +"&U="+rolejson.jscjgl[20].checked +"&D="+rolejson.jscjgl[21].checked;

            var role15 = "R="+rolejson.jscjgl[22].checked +"&C="+rolejson.jscjgl[23].checked +"&U="+rolejson.jscjgl[24].checked +"&D="+rolejson.jscjgl[25].checked;
            var role16 = "R="+rolejson.jscjgl[27].checked +"&C="+rolejson.jscjgl[28].checked +"&U="+rolejson.jscjgl[29].checked +"&D="+rolejson.jscjgl[30].checked;

            if(rolejson.jscjgl[1].checked){
                children.push({
                    "title": "组织架构",
                    "icon": "fa-group",
                    "href": "baseDataMana/RegMain2.html?"+role11
                });
            }
            if(rolejson.jscjgl[2].checked){
                // console.log(role12);
                children.push({
                    "title": "车辆信息",
                    "icon": "fa-cab",
                    "href": "baseDataMana/carInfo.html?"+role12
                });
            }
            if(rolejson.jscjgl[3].checked){
                children.push({
                    "title": "驾驶员信息",
                    "icon": "fa-address-book",
                    "href": "baseDataMana/driverMana.html?"+role13
                });
            }
            if(rolejson.jscjgl[4].checked){
                children.push({
                    "title": "客户信息",
                    "icon": "fa-microchip",
                    "href": "baseDataMana/cusMana.html?"+role14
                });
            }
            if(rolejson.jscjgl[5].checked){
                children.push({
                    "title": "线路管理",
                    "icon": "fa-magnet",
                    "href": "baseDataMana/roadMana.html?"+role15
                });
            }
            if(rolejson.jscjgl[26].checked){
                children.push({
                    "title": "油单价管理",
                    "icon": "fa-suitcase",
                    "href": "baseDataMana/OilPricemana.html?"+role16
                });
            }

            navTest[navTest.length - 1].children = children;
        }

        if(rolejson.ywgl[0].checked){
            var t2 = {
                "title": "业务管理",
                "icon": "fa-truck",
                "spread": true,
            };
            navTest.push(t2);

            var children = new Array();
//CURD 权限
            var role21 = "R="+rolejson.ywgl[8].checked +"&ORDER="+rolejson.ywgl[9].checked +"&OORDER="+rolejson.ywgl[10].checked +"&AC="+rolejson.ywgl[11].checked+"&PO="+rolejson.ywgl[12].checked+"&U="+rolejson.ywgl[13].checked+"&D="+rolejson.ywgl[14].checked+"&PD="+rolejson.ywgl[15].checked;

            var role22 = "R="+rolejson.ywgl[16].checked +"&S="+rolejson.ywgl[17].checked +"&P="+rolejson.ywgl[18].checked +"&B="+rolejson.ywgl[19].checked+"&U="+rolejson.ywgl[20].checked;
            var role23 = "R="+rolejson.ywgl[21].checked +"&DJ="+rolejson.ywgl[22].checked +"&B="+rolejson.ywgl[23].checked  ;

            var role24 = "R="+rolejson.ywgl[24].checked +"&BZ="+rolejson.ywgl[25].checked +"&B="+rolejson.ywgl[26].checked  ;

            var role25 = "R="+rolejson.ywgl[27].checked +"&C="+rolejson.ywgl[28].checked +"&B="+rolejson.ywgl[29].checked  ;
            var role26 = "R="+rolejson.ywgl[30].checked +"&CH="+rolejson.ywgl[31].checked;
            var role27 = "R="+rolejson.ywgl[32].checked +"&U="+rolejson.ywgl[33].checked;
            var role36 = "R="+rolejson.ywgl[35].checked +"&C="+rolejson.ywgl[36].checked+"&U="+rolejson.ywgl[37].checked+"&D="+rolejson.ywgl[38].checked;

            if(rolejson.ywgl[1].checked){
                children.push({
                    "title": "订单管理",
                    "icon": "fa-navicon",
                    "href": "busiMana/orderMana.html?"+role21
                });
            }
            if(rolejson.ywgl[2].checked){
                children.push({
                    "title": "车辆调度",
                    "icon": "fa-tags",
                    "href": "busiMana/vehicleSchedul.html?"+role22
                });
            }
            if(rolejson.ywgl[3].checked){
                children.push({
                    "title": "回车登记",
                    "icon": "fa-tasks",
                    "href": "busiMana/backCarRegistration.html?"+role23
                });
            }
            if(rolejson.ywgl[4].checked){
                children.push({
                    "title": "回车报账",
                    "icon": "fa-television",
                    "href": "busiMana/backCarAccount.html?"+role24
                });
            }
            if(rolejson.ywgl[5].checked){
                children.push( {
                    "title": "快捷运单",
                    "icon": "fa-search",
                    "href": "busiMana/quickWayBill.html?"+role25
                });
            }
            if(rolejson.ywgl[6].checked){
                children.push({
                    "title": "运单管理",
                    "icon": "fa-film",
                    "href": "busiMana/waybillMana.html?"+role26
                });
            }

            if(rolejson.ywgl[7].checked){
                children.push({
                    "title": "车辆管理",
                    "icon": "fa-cog",
                    "href": "busiMana/carMana.html?"+role27
                });
            }

            if(rolejson.ywgl[34].checked){
                children.push({
                    "title": "高峰业务管理",
                    "icon": "fa-thermometer-full",
                    "href": "busiMana/peakwaybillMana.html?"+role36
                });
            }

            navTest[navTest.length - 1].children = children;
        }

        if(rolejson.yyfx[0].checked){
            var t3 = {
                "title": "运营分析",
                "icon": "fa-bar-chart",
                "spread": true,
            };
            navTest.push(t3);
            var children = new Array();

            var role28 = "&STA="+ rolejson.yyfx[4].checked+"&OUT="+rolejson.yyfx[5].checked;
            var role29 =  "&STA="+rolejson.yyfx[6].checked+"&OUT="+rolejson.yyfx[7].checked;
            var role30 =  "&STA="+rolejson.yyfx[8].checked+"&OUT="+rolejson.yyfx[9].checked;



            if(rolejson.yyfx[1].checked){
                children.push( {
                    "title": "车辆费用统计",
                    "icon": "fa-star",
                    "href": "operaAnalysis/velCostStat.html?"+role28
                });
            }
            if(rolejson.yyfx[2].checked){
                children.push({
                    "title": "线路费用统计",
                    "icon": "fa-road",
                    "href": "operaAnalysis/roadCostStat.html?"+role29
                });
            }
            if(rolejson.yyfx[3].checked){
                children.push({
                    "title": "日期费用统计",
                    "icon": "fa-upload",
                    "href": "operaAnalysis/dateCostStat.html?"+role30
                });
            }

            navTest[navTest.length - 1].children = children;
        }

        if(rolejson.cwgl[0].checked){
            var t4 = {
                "title": "财务管理",
                "icon": "fa-cubes",
                "spread": true,
            };
            navTest.push(t4);

            var children = new Array();
            var role31 = "R="+rolejson.cwgl[4].checked +"&C="+rolejson.cwgl[5].checked+"&U="+rolejson.cwgl[6].checked+"&D="+rolejson.cwgl[7].checked+"&ADD="+rolejson.cwgl[8].checked;

            var role32 = "R="+rolejson.cwgl[9].checked +"&C="+rolejson.cwgl[10].checked+"&U="+rolejson.cwgl[11].checked+"&D="+rolejson.cwgl[12].checked ;
            var role33 = "R="+rolejson.cwgl[13].checked +"&C="+rolejson.cwgl[14].checked+"&U="+rolejson.cwgl[15].checked+"&D="+rolejson.cwgl[16].checked;

            if(rolejson.cwgl[1].checked){
                children.push( {
                    "title": "油卡管理",
                    "icon": "fa-inbox",
                    "href": "moneyMana/oilCardMana.html?"+role31
                });
            }
            // if(rolejson.cwgl[2].checked){
            //     children.push({
            //         "title": "运单费用",
            //         "icon": "fa-navicon",
            //         "href": "main.html"
            //     });
            // }
            if(rolejson.cwgl[2].checked){
                children.push({
                    "title": "现金报账",
                    "icon": "fa-repeat",
                    "href": "moneyMana/moneyAccount.html?"+role32
                });
            }
            if(rolejson.cwgl[3].checked){
                children.push({
                    "title": "办公室报账",
                    "icon": "fa-list-alt",
                    "href": "moneyMana/officeAccount.html?"+role33
                });
            }

            navTest[navTest.length - 1].children = children;
        }

        if(rolejson.xtgl[0].checked){
            var t5 = {
                "title": "系统管理",
                "icon": "fa-cog",
                "spread": true,
            };
            navTest.push(t5);
            var children = new Array();
            var role34 = "R="+rolejson.xtgl[3].checked +"&C="+rolejson.xtgl[4].checked+"&U="+rolejson.xtgl[5].checked+"&D="+rolejson.xtgl[6].checked+"&RE="+rolejson.xtgl[7].checked;
            var role35 = "R="+rolejson.xtgl[8].checked +"&C="+rolejson.xtgl[9].checked+"&U="+rolejson.xtgl[10].checked+"&D="+rolejson.xtgl[11].checked+"&MN="+rolejson.xtgl[12].checked;
            if(rolejson.xtgl[1].checked){
                children.push(  {
                    "title": "用户管理",
                    "icon": "fa-paperclip",
                    "href": "systemMana/userMana.html?"+role34
                });
            }
            if(rolejson.xtgl[2].checked){
                children.push({
                    "title": "角色管理",
                    "icon": "fa-check",
                    "href": "systemMana/roleMana.html?"+role35
                });
            }

            children.push({
                "title": "修改密码",
                "icon": "fa-lock",
                "href": "systemMana/alterMana.html"
            });

            children.push({
                "title": "数据可视化设置",
                "icon": "fa-phone",
                "href": "systemMana/picMgmt.html"
            });

            if(rolejson.xtgl[5].checked){
            children.push({
                "title": "用户注册审核",
                "icon": "fa-street-view",
                "href": "appcheck/registeCheck.html"
            });
            }
            navTest[navTest.length - 1].children = children;
        }else {
            var t5 = {
                "title": "系统管理",
                "icon": "fa-cubes",
                "spread": true,
            };
            navTest.push(t5);
            var children = new Array();
            children.push({
                "title": "修改密码",
                "icon": "fa-tree-conifer",
                "href": "systemMana/alterMana.html"
            });
            children.push({
                "title": "数据可视化设置",
                "icon": "fa-subtitles",
                "href": "systemMana/picMgmt.html"
            });
            navTest[navTest.length - 1].children = children;
        }

        //设置navbar
        navbar.set({
            spreadOne: true,
            elem: '#admin-navbar-side',
            cached: true,
            data: navTest
            /*cached:true,
            url: 'datas/nav.json'*/
        });
        //渲染navbar
        navbar.render();
        //监听点击事件
        navbar.on('click(side)', function (data) {
            tab.tabAdd(data.field);
        });
        //清除缓存
        $('#clearCached').on('click', function () {
            navbar.cleanCached();
            layer.alert('清除完成!', { icon: 1, title: '系统提示' }, function () {
                location.reload();//刷新
            });
        });
    }
});
    //获取角色json

    //模拟数据


//请求rolejson
    // $.ajax({
    //     url: contextPath+"/currentUser/role" ,
    //     type: "GET",
    //     contentType:"application/json;charset=utf-8",
    //     success: function(res) {
    //         var roleJson = res.roleJson;
    //         roleJson.jcsjglNodes
    //     }
    // });



    $('.admin-side-toggle').on('click', function () {
        var sideWidth = $('#admin-side').width();
        if (sideWidth === 200) {
            $('#admin-body').animate({
                left: '0'
            }); //admin-footer
            $('#admin-footer').animate({
                left: '0'
            });
            $('#admin-side').animate({
                width: '0'
            });
        } else {
            $('#admin-body').animate({
                left: '200px'
            });
            $('#admin-footer').animate({
                left: '200px'
            });
            $('#admin-side').animate({
                width: '200px'
            });
        }
    });
    $('.admin-side-full').on('click', function () {
        var docElm = document.documentElement;
        //W3C  
        if (docElm.requestFullscreen) {
            docElm.requestFullscreen();
        }
        //FireFox  
        else if (docElm.mozRequestFullScreen) {
            docElm.mozRequestFullScreen();
        }
        //Chrome等  
        else if (docElm.webkitRequestFullScreen) {
            docElm.webkitRequestFullScreen();
        }
        //IE11
        else if (elem.msRequestFullscreen) {
            elem.msRequestFullscreen();
        }
        layer.msg('按Esc即可退出全屏');
    });

    $('#setting').on('click', function () {
        tab.tabAdd({
            href: '/Manage/Account/Setting/',
            icon: 'fa-gear',
            title: '设置'
        });
    });

    //锁屏
    $(document).on('keydown', function () {
        var e = window.event;
        if (e.keyCode === 76 && e.altKey) {
            //alert("你按下了alt+l");
            lock($, layer);
        }
    });
    $('#lock').on('click', function () {
        lock($, layer);
    });

    //手机设备的简单适配
    var treeMobile = $('.site-tree-mobile'),
        shadeMobile = $('.site-mobile-shade');
    treeMobile.on('click', function () {
        $('body').addClass('site-mobile');
    });
    shadeMobile.on('click', function () {
        $('body').removeClass('site-mobile');
    });
});

var isShowLock = false;
function lock($, layer) {
    if (isShowLock)
        return;
    //自定页
    layer.open({
        title: false,
        type: 1,
        closeBtn: 0,
        anim: 6,
        content: $('#lock-temp').html(),
        shade: [0.9, '#393D49'],
        success: function (layero, lockIndex) {
            isShowLock = true;
            //给显示用户名赋值
            //layero.find('div#lockUserName').text('admin');
            //layero.find('input[name=username]').val('admin');
            layero.find('input[name=password]').on('focus', function () {
                var $this = $(this);
                if ($this.val() === '输入密码解锁..') {
                    $this.val('').attr('type', 'password');
                }
            })
                .on('blur', function () {
                    var $this = $(this);
                    if ($this.val() === '' || $this.length === 0) {
                        $this.attr('type', 'text').val('输入密码解锁..');
                    }
                });
            //在此处可以写一个请求到服务端删除相关身份认证，因为考虑到如果浏览器被强制刷新的时候，身份验证还存在的情况			
            //do something...
            //e.g. 

            $.getJSON('/Account/Logout', null, function (res) {
                if (!res.rel) {
                    layer.msg(res.msg);
                }
            }, 'json');

            //绑定解锁按钮的点击事件
            layero.find('button#unlock').on('click', function () {
                var $lockBox = $('div#lock-box');

                var userName = $lockBox.find('input[name=username]').val();
                var pwd = $lockBox.find('input[name=password]').val();
                if (pwd === '输入密码解锁..' || pwd.length === 0) {
                    layer.msg('请输入密码..', {
                        icon: 2,
                        time: 1000
                    });
                    return;
                }
                unlock(userName, pwd);
            });
			/**
			 * 解锁操作方法
			 * @param {String} 用户名
			 * @param {String} 密码
			 */
            var unlock = function (un, pwd) {
                console.log(un, pwd);
                //这里可以使用ajax方法解锁
                $.post('/Account/UnLock', { userName: un, password: pwd }, function (res) {
                    //验证成功
                    if (res.rel) {
                        //关闭锁屏层
                        layer.close(lockIndex);
                        isShowLock = false;
                    } else {
                        layer.msg(res.msg, { icon: 2, time: 1000 });
                    }
                }, 'json');
                //isShowLock = false;
                //演示：默认输入密码都算成功
                //关闭锁屏层
                //layer.close(lockIndex);
            };
        }
    });
};